package com.yanqu.road.dao.impl.union.parergon;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.union.parergon.UnionBussinessman;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class UnionBussinessmanDaoImpl extends TempDataObjectDao<UnionBussinessman> {

    public Integer getMaxBussinessId() {
        String sql = "select ifnull(max(bussinessId),0) as maxId from t_u_union_bussinessman";
        ResultSet rs = null;
        CallableStatement call = null;
        Integer maxId = 0;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(sql);
            rs = call.executeQuery();
            while (rs.next()) {
                maxId = rs.getInt("maxId");
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return maxId;
    }

    public Map<Integer, UnionBussinessman> getUnionBussinessmanMap() {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, UnionBussinessman> dataMap = new ConcurrentHashMap<>();
        String sql = "select * from t_u_union_bussinessman where disappearTime > ?";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(sql);
            long now = System.currentTimeMillis() / 1000;
            int i = 1;
            call.setLong(i++, now);
            rs = call.executeQuery();
            while (rs.next()) {
                UnionBussinessman data = parse(rs);
                dataMap.put(data.getBussinessId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }

    @Override
    protected boolean addData(UnionBussinessman data) {
        PreparedStatement call = null;
        String sql = "INSERT INTO t_u_union_bussinessman(bussinessId,unionId,disappearTime,productId,bargainPrice,bargainTimes,bargainNum," +
                "createTime,bargainValues) VALUES (?,?,?,?,?,?,?,?,?)";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareStatement(sql);
            int i = 1;
            call.setInt(i++, data.getBussinessId());
            call.setString(i++, data.getUnionId());
            call.setLong(i++, data.getDisappearTime());
            call.setInt(i++, data.getProductId());
            call.setLong(i++, data.getBargainPrice());
            call.setInt(i++, data.getBargainTimes());
            call.setInt(i++, data.getBargainNum());
            call.setLong(i++, data.getCreateTime());
            call.setString(i++, data.getBargainValuesStr());
            return call.executeUpdate() > 0;
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return false;
    }

    @Override
    protected boolean updateData(UnionBussinessman data) {
        PreparedStatement call = null;
        String sql = "UPDATE t_u_union_bussinessman SET unionId=?,disappearTime=?,productId=?,bargainPrice=?,bargainTimes=?,bargainNum=?," +
                "createTime=?,bargainValues=? WHERE bussinessId=?";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareStatement(sql);
            int i = 1;
            call.setString(i++, data.getUnionId());
            call.setLong(i++, data.getDisappearTime());
            call.setInt(i++, data.getProductId());
            call.setLong(i++, data.getBargainPrice());
            call.setInt(i++, data.getBargainTimes());
            call.setInt(i++, data.getBargainNum());
            call.setLong(i++, data.getCreateTime());
            call.setString(i++, data.getBargainValuesStr());
            call.setInt(i++, data.getBussinessId());
            return call.executeUpdate() > 0;
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return false;
    }

    @Override
    protected UnionBussinessman parseData(ResultSet rs) throws SQLException {
        UnionBussinessman data = new UnionBussinessman();
        data.setBussinessId(rs.getInt("bussinessId"));
        data.setUnionId(rs.getString("unionId"));
        data.setDisappearTime(rs.getLong("disappearTime"));
        data.setProductId(rs.getInt("productId"));
        data.setBargainPrice(rs.getLong("bargainPrice"));
        data.setBargainTimes(rs.getInt("bargainTimes"));
        data.setBargainNum(rs.getInt("bargainNum"));
        data.setCreateTime(rs.getLong("createTime"));
        String value = rs.getString("bargainValues");
        if(!StringUtils.isNullOrEmpty(value)){
            data.setBargainValues(StringUtils.stringToIntegerList(value, ","));
        }
        return data;
    }
}
